package com.sjq.yqgl.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import com.sjq.yqgl.dao.YqfjDao;
import com.sjq.common.dao.GenericDao;
import com.sjq.common.util.PageResults;
import com.sjq.yqgl.entity.Yqfj;

@Repository
public class YqfjDaoImpl extends GenericDao<Yqfj> implements YqfjDao {

	 @Override
	 public PageResults<?> selectYqfjByPage(int pageNo, int pageSize,Yqfj yqfj){
	 	StringBuilder paramSql = new StringBuilder();
		List params = new ArrayList();
		//查询条件
		/**/
		if (yqfj.getFjlx() != null&& !(yqfj.getFjlx().equals(""))) {
			paramSql.append(" and fjlx=? ");
			params.add(yqfj.getFjlx().trim());
		}
		if (yqfj.getXxzjbh() != null&& !(yqfj.getXxzjbh().equals(""))) {
			paramSql.append(" and xxzjbh=? ");
			params.add(yqfj.getXxzjbh().trim());
		}
		if (yqfj.getShr() != null&& !(yqfj.getShr().equals(""))) {
			paramSql.append(" and shr=? ");
			params.add(yqfj.getShr().trim());
		}
		if (yqfj.getRbr() != null&& !(yqfj.getRbr().equals(""))) {
			paramSql.append(" and rbr=? ");
			params.add(yqfj.getXxzjbh().trim());
		}
		if (yqfj.getTbdw() != null&& !(yqfj.getTbdw().equals(""))) {
			paramSql.append(" and instr(tbdw,?)>0 ");
			params.add(yqfj.getXxzjbh().trim());
		}
		if (yqfj.getTbsj_s() != null&& !(yqfj.getTbsj_s().equals(""))) {
			paramSql.append(" and (tbsj>?  or tbsj=?)");
			params.add(yqfj.getTbsj_s().trim());
			params.add(yqfj.getTbsj_s().trim());
		}
		if (yqfj.getTbsj_e() != null&& !(yqfj.getTbsj_e().equals(""))) {
			paramSql.append(" and (tbsj<?  or tbsj=?) ");
			params.add(yqfj.getTbsj_e().trim());
			params.add(yqfj.getTbsj_e().trim());
		}
		
		if (yqfj.getDjrjh() != null&& !(yqfj.getDjrjh().equals(""))) {
			paramSql.append(" and djrjh=? ");
			params.add(yqfj.getDjrjh().trim());
		}
		String sql_fenye="select XXZJBH,TBDW, ofjmc,fjmc,title,TBSJ,RBR,SHR,FJLJ,to_char(DJSJ,'yyyy-MM-dd hh24:mi:ss') as DJSJ,FJLX from T_YQ_BSFJ where DJBGBS <>3 " + paramSql.toString() ;
		PageResults<Map<String, String>> results = new PageResults<Map<String, String>>();
		StringBuilder pageSql = new StringBuilder()
				.append("select * from ")
				.append(" (select tmp_1.*, rownum as rowindex from ("+sql_fenye+") tmp_1 ) tmp_2 ")
				.append(" where rowindex between ")
				.append(((pageNo-1)*pageSize + 1))
				.append(" and ")
				.append(((pageNo - 1) * pageSize + pageSize));
		List<Map<String, String>> list=new ArrayList<Map<String, String>>();
		try {
			list = this.getListBySql(pageSql.toString(),params.toArray());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql="select count(1) as num from T_YQ_BSFJ where DJBGBS <>3  "+paramSql.toString()+" ";
		SqlRowSet sqlRowSet = this.getJdbcTemplate().queryForRowSet(sql,params.toArray());
		int count = 0;
		while(sqlRowSet.next()){
			count = sqlRowSet.getInt("num");
		}
		results.setTotalCount(count);
        results.setPageNo(pageNo);
        results.setPageSize(pageSize);
		results.setResults(list);
		return results;	
	 }
	 @Override
	 public int addYqfj(Yqfj yqfj){
	 	String sql="insert into T_YQ_BSFJ ( QZRMC,OFJMC,FJMC,djdw,djdwmc,djrjh,title,xxzjbh,tbdw,tbsj,rbr,shr,fjlj,fjlx) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)  ";
	 	Object [] params={yqfj.getQzrymc(),yqfj.getOfjmc(),yqfj.getFjmc(),yqfj.getDjdw(),yqfj.getDjdwmc(),yqfj.getDjrjh(),yqfj.getTitle(),yqfj.getXxzjbh(),yqfj.getTbdw(),yqfj.getTbsj(),yqfj.getRbr(),yqfj.getShr(),yqfj.getFjlj(),yqfj.getFjlx()};
	 	return this.getJdbcTemplate().update(sql.toString(),params);
	 }
	 
	 @Override
	 public int deleteYqfj(String yqfjId){
		 String sql="update T_YQ_BSFJ set djbgbs=3 where xxzjbh=?";
		 Object [] params={ yqfjId};
		 return this.getJdbcTemplate().update(sql.toString(),params);
	 }
	 
	  @Override
	 public int updateYqfj(Yqfj yqfj){
	 	String sql="update T_YQ_BSFJ set XXZJBH=?,TBDW=?,TBSJ=?,RBR=?,SHR=?,FJLJ=?,DJSJ=?,FJLX=?  where xxzjbh=?";
	 	Object [] params={yqfj.getXxzjbh(),yqfj.getTbdw(),yqfj.getTbsj(),yqfj.getRbr(),yqfj.getShr(),yqfj.getFjlj(),yqfj.getDjsj(),yqfj.getFjlx()};
	 	return this.getJdbcTemplate().update(sql.toString(),params);
	 }
	 
	 @Override
	 public List<Map<String, String>> findYqfjByListAll(Yqfj yqfj){
		StringBuilder paramSql = new StringBuilder();
		List params = new ArrayList();
		//查询条件
		if (yqfj.getXxzjbh() != null&& !(yqfj.getXxzjbh().equals(""))) {
			paramSql.append(" and xxzjbh=? ");
			params.add(yqfj.getXxzjbh().trim());
		}
		String sql="select XXZJBH,TBDW,to_char(TBSJ,'yyyy-MM-dd hh24:mi:ss') as TBSJ,RBR,SHR,FJLJ,to_char(DJSJ,'yyyy-MM-dd hh24:mi:ss') as DJSJ,FJLX from T_YQ_BSFJ where DJBGBS <>3  "+ paramSql.toString() ;
		List<Map<String, String>> list=new ArrayList<Map<String, String>>();
		try {
			list = this.getListBySql(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
}